package com.df.bpm.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import com.df.bpm.dto.DtoDepartmentRole;
import com.df.bpm.dto.DtoDepartmentRole4create;
import com.df.bpm.entity.Role;
import com.df.bpm.entity.RoleDepartment;
import com.df.bpm.service.impl.DtoDepartmentRoleServiceImpl;
import com.df.bpm.service.impl.RoleDepartmentServiceImpl;
import com.df.bpm.service.impl.RoleServiceImpl;
import com.df.common.entity.Pagination;
import com.df.common.utils.CommonUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;



/**
 * 角色控制器
 */
@Slf4j
@Tag(name = "角色控制器")
@RequestMapping("/role")
@RequiredArgsConstructor
@RestController
@SaCheckLogin
public class RoleController {
    private final RoleServiceImpl roleServiceImpl;
    private final DtoDepartmentRoleServiceImpl dtoDepartmentRoleServiceImpl;
    private final RoleDepartmentServiceImpl roleDepartmentServiceImpl;

    /**
     * 查询角色
     * @param pagination 分页信息
     * @param temp 角色线索
     * @return  角色列表
     */
    @Operation(summary = "查询角色", description = "查询角色")
    @GetMapping("")
    @SaIgnore
    public ResponseEntity<?> getRole(Pagination pagination, Role temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(roleServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(roleServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 查询部门角色dto
     * @param pagination 分页信息
     * @param temp 部门角色dto线索
     * @return  部门角色dto列表
     */
    @Operation(summary = "查询部门角色dto", description = "查询部门角色dto")
    @GetMapping("dtoDepartmentRole")
    @SaIgnore
    public ResponseEntity<?> getDtoDepartmentRole(Pagination pagination, DtoDepartmentRole temp) {
        if (CommonUtils.varIsNotBlank(pagination.getPageNum(), pagination.getPageSize())) {
            PageHelper.startPage(pagination.getPageNum(), pagination.getPageSize());
            return ResponseEntity.ok(new PageInfo<>(dtoDepartmentRoleServiceImpl.getList4Like(temp)));
        } else {
            return ResponseEntity.ok(dtoDepartmentRoleServiceImpl.getList4Like(temp));
        }
    }

    /**
     * 创建部门角色
     * @param temp   角色信息
     * @return  新增成功后的角色信息
     */
    @Operation(summary = "创建部门角色", description = "创建部门角色")
    @PostMapping("departmentRole")
    @SaIgnore
    public ResponseEntity<?> newDepartmentRole(@RequestBody DtoDepartmentRole4create temp) {
        Role newRole=new Role();
        if(temp==null || temp.getRoleName()==null){
            throw new RuntimeException("新角色名不能为空");
        }else{
            newRole.setName(temp.getRoleName());
            roleServiceImpl.add(newRole);
            RoleDepartment newShip=new RoleDepartment();
            newShip.setDepartmentId(temp.getDepartmentId());
            newShip.setRoleId(newRole.getId());
            roleDepartmentServiceImpl.add(newShip);
            return ResponseEntity.ok(newRole);
        }
    }

    /**
     * 新增角色
     * @param temp   角色信息
     * @return  新增成功后的角色信息
     */
    @Operation(summary = "新增角色", description = "新增角色")
    @PostMapping("")
    @SaIgnore
    public ResponseEntity<?> newRole(@RequestBody Role temp) {
        roleServiceImpl.add(temp);
        return ResponseEntity.ok(temp);
    }

}
